//
//  MZCTabBarController.m
//  CheekiOS
//
//  Created by mac on 2017/5/8.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "MZCTabBarController.h"
#import "MZCNavigationController.h"
#import "MZCTabBar.h"

#import "HomeViewController.h"
#import "FriendViewController.h"
#import "CheekVodepViewController.h"
#import "UserViewController.h"


@interface MZCTabBarController ()

@end

@implementation MZCTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化所有控制器
    [self setUpChildVC];
    
    // 创建tabbar中间的tabbarItem
//    [self setUpMidelTabbarItem];
    
}

#pragma mark -创建tabbar中间的tabbarItem

//- (void)setUpMidelTabbarItem {
//    
//    MZCTabBar *tabBar = [[MZCTabBar alloc] init];
//    [self setValue:tabBar forKey:@"tabBar"];
//    
//    __weak typeof(self) weakSelf = self;
//    [tabBar setDidClickPublishBtn:^{
//        
//        TBPlusViewController *hmpositionVC = [[TBPlusViewController alloc] init];
//        MZCNavigationController *nav = [[MZCNavigationController alloc] initWithRootViewController:hmpositionVC];
//        [weakSelf presentViewController:nav animated:YES completion:nil];
//        
//    }];
//    
//}

#pragma mark -初始化所有控制器

- (void)setUpChildVC {
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    [self setChildVC:homeVC title:@"" image:@"tabbar_home_normal" selectedImage:@"tabbar_home_select"];
    
   
    CheekVodepViewController *videoVC = [[CheekVodepViewController alloc]init];
    [self setChildVC:videoVC title:@"" image:@"tabbar_voucher_normal" selectedImage:@"tabbar_voucher_select"];
    
    FriendViewController *friendVC = [[FriendViewController alloc] init];
    [self setChildVC:friendVC title:@"" image:@"tabbar_find_normal" selectedImage:@"tabbar_find_select"];
    
    
    UserViewController *userVC = [[UserViewController alloc] init];
    [self setChildVC:userVC title:@"" image:@"tabbar_my_normal" selectedImage:@"tabbar_my_select"];
    
    
    
}

- (void) setChildVC:(UIViewController *)childVC title:(NSString *) title image:(NSString *) image selectedImage:(NSString *) selectedImage {
    //tabBar 边缘线去掉
//    CGRect rect = CGRectMake(0, 0, KScreenWidth, 1);
//    
//    UIGraphicsBeginImageContext(rect.size);
//    
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    
//    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
//    
//    CGContextFillRect(context, rect);
//    
//    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
//    
//    UIGraphicsEndImageContext();
//
//    [self.tabBar setShadowImage:img];
    //去掉边线方法
    self.navigationController.navigationBar.alpha=0.5; 
    [[UITabBar appearance] setShadowImage:[[UIImage alloc]init]];
    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc]init]];
    
    childVC.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
    childVC.tabBarItem.title = title;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor blackColor];
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    [childVC.tabBarItem setTitleTextAttributes:dict forState:UIControlStateNormal];
    childVC.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    MZCNavigationController *nav = [[MZCNavigationController alloc] initWithRootViewController:childVC];
    [self addChildViewController:nav];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    NSLog(@"item name = %@", item.title);
    NSInteger index = [self.tabBar.items indexOfObject:item];
    [self animationWithIndex:index];
    if([item.title isEqualToString:@"发现"])
    {
        // 也可以判断标题,然后做自己想做的事<img alt="得意" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/proud.gif" />
    }
}
- (void)animationWithIndex:(NSInteger) index {
    NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.2;
    pulse.repeatCount= 1;
    pulse.autoreverses= YES;
    pulse.fromValue= [NSNumber numberWithFloat:0.7];
    pulse.toValue= [NSNumber numberWithFloat:1.3];
    [[tabbarbuttonArray[index] layer]
     addAnimation:pulse forKey:nil];
}

@end
